﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace TCS.Web.uc
{
    /// <summary>
    /// 两个属性，1.id  2.Name
    /// </summary>
    public partial class selectarea : System.Web.UI.UserControl
    {
        private string _areaid;
        private string toadd = "├";
        private string _onclick = string.Empty;

        public string Onclick
        {
            get { return _onclick; }
            set { _onclick = value; }
        }
        public string Areaid
        {
            get { return this.dropParent.SelectedValue; }
           
        }
        private string _areaname;

        public string Areaname
        {
            get { return GetAreaName(int.Parse(this.dropParent.SelectedValue)); }
           
        }
        private string _findselected;
        public string FindIdSelected
        {
            set { _findselected = value;}
        }
       

       
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                LoadData();
                this.dropParent.Attributes.Add("onchange", this.Onclick);
                
                try
                {
                    if (_findselected != null && _findselected != "")
                    {
                        this.dropParent.Items.FindByValue(_findselected).Selected = true;
                    }
                }
                catch
                { }
            }
        }
        void LoadData()
        {
            LoadChildData(0);
        }
        private void LoadChildData(int parentid)
        {
            DataSet ds = new TCS.BLL.Gsp_Administrative().GetListNew(0, "parentid=" + parentid.ToString(), "addid");
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    this.dropParent.Items.Add(new ListItem(toadd + "" + ds.Tables[0].Rows[i]["AddName"].ToString(), ds.Tables[0].Rows[i]["ADDID"].ToString()));
                    toadd += "─┴";
                    LoadChildData(int.Parse(ds.Tables[0].Rows[i]["ADDID"].ToString()));
                    toadd = toadd.Substring(0, toadd.Length - 2);
                }
            }
        }
        private string GetAreaName(int addid)
        {
            DataSet ds = new TCS.BLL.Gsp_Administrative().GetListNew(1, "addid=" + addid, "addid");
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0]["addName"].ToString();
            }
            return "没有数据";
        }

       
    }
}